﻿

var InitiateExpandableDataTable = function () {
    return {
        init: function () {
           
            ///*
            // * Insert a 'details' column to the table
            // */
            //var nCloneTh = document.createElement('th');
            ////var nCloneTd = document.createElement('td');
            ////nCloneTd.innerHTML = '<i class="fa fa-plus-square-o row-details"></i>';

            //$('#expandabledatatable thead tr').each(function () {
            //    this.insertBefore(nCloneTh, this.childNodes[0]);
            //});

            /*
             * 初始化表格
             */
            var oTable = $('#expandabledatatable').dataTable({
                "sDom": "Tflt<'row DTTTFooter'<'col-sm-6'i><'col-sm-6'p>>",
                "aoColumnDefs": [
                    {
                        "bSortable": false, "aTargets": [0],
                        "render": function (data, type, full) {
                            return '<button type="button" class="btn btn-warning btn-sm">搞毛</button>'; ///<input type="checkbox" class="colored-success" checked="checked" />
                        }
                    },
                     //{
                     //    "bSortable": false,
                     //    "aTargets": [0],
                     //    "sClass": 'select-checkbox'
                         
                     //},
                    { "aTargets": [1], "data": "TaskName", "title": "任务名称" },
                    { "aTargets": [2], "data": "CronExpression", "title": "表达式" },
                    { "aTargets": [3], "data": "EndTime", "title": "最近一次运行时间" },
                    { "aTargets": [4], "data": "LastTime", "title": "下次运行时间" },
                    {
                        "bSortable": false, "aTargets": [5], "title": "运行状态",
                        "render": function (data, type, full) {
                            if (full["Allowused"] == true) {
                                return '<button type="button" class="btn btn-success btn-sm">运行</button>';
                            }
                            else {
                                return '<button type="button" class="btn btn-warning btn-sm">停止</button>';
                            }
                        }
                    },
                    { "aTargets": [6], "data": "Remark", "title": "说明" },

                ],
                //select: {
                //    style: 'os',
                //    selector: 'td:first-child'
                //},
                //order: [[ 1, 'asc' ]],
                "bServerSide": true,
                "sAjaxSource": "/Task/GetAllTask",
                "aaSorting": [[1, 'asc']],
                "aLengthMenu": [
                   [5, 15, 20, -1],
                   [5, 15, 20, "All"]
                ],
                "iDisplayLength": 5,
                "searching": false,
                "bLengthChange": false,
                "language": {
                    "sProcessing": "正在加载数据...",
                    "sInfoEmpty": "记录数为0",
                    "sInfoFiltered": " 从 _MAX_ 条过滤",
                    "sZeroRecords": "没有您要搜索的内容",
                    "search": "",
                    "sLengthMenu": "_MENU_",
                    "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                    "oPaginate": {
                        "sPrevious": "上一页",
                        "sNext": "下一页",
                    }
                },
                "bAutoWidth": true,
                "fnServerData": function (sSource, aoData, fnCallback) {
                    $.ajax({
                        "type": 'post',
                        "url": sSource,
                        "dataType": "json",
                        "data": {
                            aoData: JSON.stringify(aoData)
                        },
                        "success": function (resp) {
                            fnCallback(resp);
                        }
                    });
                }
            });

            //删除一行
            $('#expandabledatatable').on("click", 'a.delete', function (e) {
                e.preventDefault();

                if (confirm("确定删除吗?") == false) {
                    return;
                }
                var nRow = $(this).parents('tr')[0];
                var data = oTable.fnGetData(nRow)
                $.ajax({
                    "type": 'post',
                    "url": '/Task/DeleteTask',
                    "data": { "id": data["Id"] },
                    "success": function (data) {
                        oTable.fnDeleteRow(nRow);
                    }
                })
            
            });

            $('#expandabledatatable tbody').on('click', 'tr', function () {
                var table = $('#example').DataTable();
                if ($(this).hasClass('selected')) {
                    $(this).removeClass('selected');
                }
                else {
                    table.$('tr.selected').removeClass('selected');
                    $(this).addClass('selected');
                }
               
            });
        }


    };

   
}();

